<?php 

class User_model extends CI_Model{
	function __construct()
	{
	    parent::__construct();	    
	}
	//创建机票订单
	public function registeruser($data,$userdata)
	{
		
		//检查是否有重复数据
		
		
		if($this->check_name($data["useraccount"]))
		{
			return array(false,"this account have been regist");
		}

		
          $inf_userdata=array(
	         	"useraccount" => $data["useraccount"],
			"username" =>  $data["username"],
			"userpassword" => md5($data["userpassword"]),
			"birthday" => date("Y-m-d",strtotime($data["birthday"])),
			"tel1" => $data["tel"],
			"mobilephone" => $data["mobilephone"],
			"address" => $data["address"],
			"Email"	=> $data["email"],
			"AgentCode" => $userdata["WEB_AgentCode"],
			"AgentName" => $userdata["WEB_AgentName"]			
          );
          //插入订单表map_order
		$this->db->insert('inf_user', $inf_userdata);
		//插入客人信息表bok_cps
		
		
		return array(true,"Register Success");		

	}
	//检查用户名
	function check_name($name)
	{
		$query = $this->db->get_where('inf_user',array('useraccount' => $name));
	     if ($row = $query->row_array()){
	          return true;
	     }
        	return false;
	}
	
	/**
	 * 查询该用户，返回用户信息
	 *
	 *
	 */	
	function check_customer($useraccount,$userpassword,$AgentCode)
	{
        $query = $this->db->get_where('inf_user',array('useraccount' => $useraccount,
	        'userpassword' => md5($userpassword),
	        'AgentCode' => $AgentCode
        ));
      
        if ($row = $query->row_array()){
            return $row;
        }
        return array();
	}
	
	 function update_last_login($useraccount)
	{
		ini_set("date.timezone","Asia/Shanghai");
		$datetime = date('Y-m-d H:i:s');
		$this->db->set('last_login', $datetime);		
		$this->db->set('logintime', "logintime+1");
        	$this->db->where('useraccount', $useraccount);
        	return $this->db->update('inf_user');
	}
	
	//查询客户
	public function query_customer($querydata,$user)
	{
		$this->db->where("AgentCode",$user["uadmin_account"]);
		
		if($querydata["tel"]!=""){
			$this->db->like("tel1",$querydata["tel"]);
			$this->db->or_like("mobilephone",$querydata["tel"]);
			
		}
		if($querydata["address"]!=""){
			$this->db->like("address",$querydata["address"]);
		}
		if($querydata["username"]!=""){
			$this->db->like("username",$querydata["username"]);
		}
		
		
		if($querydata["registerDate1"]!="")
		{
			$this->db->where("DateDiff('".$querydata["registerDate1"]."',registerdate)<=0");
		}
		
		if($querydata["registerDate2"]!="")
		{
			$this->db->where("DateDiff(registerdate,'".$querydata["registerDate2"]."')<=0");
		}
		
		$query = $this->db->get('inf_user');
		
		return $query->result();      
	}

	//检查用户名
	function get_customer($sAccount)
	{
		$query = $this->db->get_where('inf_user',array('useraccount' => $sAccount));
	     if ($row = $query->row_array()){
	          return $row;
	     }
        	return false;
	}
	
	
	//新增数据
	function add_customer($data,$user)
	{
		
		//检查是否有重复数据
		if($this->check_name($data["useraccount"]))
		{
			return array(false,"this account have been regist");
		}

		
          $inf_userdata=array(
	         	"useraccount" => $data["useraccount"],
			"username" =>  $data["username"],
			"userpassword" => md5("888888"),
			"birthday" => $data["birthday"],
			"tel1" => $data["tel"],
			"mobilephone" => $data["mobilephone"],
			"address" => $data["address"],
			"AgentCode" => $user["uadmin_account"],
			"AgentName" => $user["uadmin_name"]
          );
         
		$this->db->insert('inf_user', $inf_userdata);
		
		return true;

	}
	
	//保存数据
	function edit_customer($data,$user)
	{
		
		$agentdata=array(
			"username" =>  $data["username"],
			"birthday" => $data["birthday"],
			"tel1" => $data["tel"],
			"mobilephone" => $data["mobilephone"],
			"address" => $data["address"],
			"Email" 	=> $data["email"]
		);
		$this->db->where('useraccount', $data["useraccount"]);
		return $this->db->update('inf_user', $agentdata); 
	}
	
	//修改资料或者密码
	function modifyuserdata($user,$modifydata)
	{
		$this->db->where('useraccount', $user);
		return $this->db->update('inf_user', $modifydata); 
	}
}